package unclassified.medium;

import tree.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class LCOF_26 {
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if (B == null) {
            return false;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(A);
        TreeNode curA, curB = B;
        a:while (!queue.isEmpty()){
            int size = queue.size();
            while (size-- > 0) {
                TreeNode poll = queue.poll();
                if (poll.val == curB.val) {
                    curA = poll;
                    break a;
                }
                if (poll.left != null) {
                    queue.offer(poll.left);
                }
                if (poll.right != null) {
                    queue.offer(poll.right);
                }
            }
        }
        return true;
    }
}
